package com.haima.sage.bigdata.api.service.impl;

import com.haima.sage.bigdata.api.dao.ComputeTaskRepository;
import com.haima.sage.bigdata.api.entity.ComputeTask;
import com.haima.sage.bigdata.api.service.ComputeTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

@Service
public class ComputeTaskServiceImpl implements ComputeTaskService {
    private final ComputeTaskRepository repository;

    @Autowired
    public ComputeTaskServiceImpl(ComputeTaskRepository repository) {
        this.repository = repository;
    }

    @Override
    public Optional<ComputeTask> findById(String id) {
        return this.repository.findById(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(ComputeTask task) {
        task.setId(UUID.randomUUID().toString());
        task.setCreateDate(new Date());
        task.setUpdateDate(task.getCreateDate());
        this.repository.save(task);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateBase(ComputeTask task) {
         this.repository.updateBase(task.getId(), task.getName(),
                task.getData(), task.getDescription(), task.getUpdateDate());
    }
    @Override
    @Transactional(readOnly = false)
    public void updateData(ComputeTask task) {
        this.repository.updateData(task.getId(), task.getData(), task.getUpdateDate());
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteById(String id) {
        this.repository.deleteById(id);
    }

    @Override
    public Page<ComputeTask> find(Pageable pageable) {
        return this.repository.findAll(pageable);
    }
}
